﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module;
using System.Data.SqlClient;
using Qmaker.DAL;
using System.Data;

namespace DLL
{
    /// <summary>
    /// 
    /// </summary>
    public class DArea
    {

        #region SQL语句

        #region 查询

        private const string SQL_SELECT = "select  AreaId,AreaCode,AreaName,ParentId from Area where ParentId=@ParentId";

        private const string SQL_SELECT_ALL = "select  AreaId,AreaCode,AreaName,ParentId from Area";

        #endregion

        #endregion

        #region 公共接口

        /// <summary>
        /// 根据上级行政Id获取所有下级所有行政地区
        /// </summary>
        /// <returns></returns>
        public List<AreaInfo> GetAreaByParentId(long parentId)
        {
            List<AreaInfo> result = new List<AreaInfo>();
            SqlParameter[] parms = {
                    new SqlParameter("@ParentId",SqlDbType.BigInt)
                };
            parms[0].Value = parentId;
            AreaInfo menu = null;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT, parms))
            {
                while (reader.Read())
                {
                    menu = GetInstanceByDataReader(reader);
                    if (menu != null)
                        result.Add(menu);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取所有行政区域
        /// </summary>
        /// <returns></returns>
        public List<AreaInfo> GetALLArea()
        {
            List<AreaInfo> result = new List<AreaInfo>();
            
            AreaInfo menu = null;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT_ALL, null))
            {
                while (reader.Read())
                {
                    menu = GetInstanceByDataReader(reader);
                    if (menu != null)
                        result.Add(menu);
                }
            }
            return result;
        }

       





        #endregion

        #region 私有接口

        private AreaInfo GetInstanceByDataReader(SqlDataReader reader)
        {
            // AreaId,AreaCode,AreaName,ParentId

            AreaInfo current = new AreaInfo();
            current.AreaId = reader.IsDBNull(0) ? 0 : reader.GetInt64(0);
            current.AreaCode = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);
            current.AreaName = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
            current.ParentId = reader.IsDBNull(3) ? 0 : reader.GetInt32(3);

            return current;
        }
        /// <summary>
        /// 检查属性值是否为空
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private SqlParameter[] SetInsertParmsCheck(SqlParameter[] param)
        {
            for (int i = 0; i < param.Length; i++)
            {
                if (param[i].Value == null)
                {
                    param[i].Value = DBNull.Value;
                }
                if (param[i].DbType == DbType.DateTime && param[i].Value.Equals(DateTime.MinValue))
                {
                    param[i].Value = DBNull.Value;
                }
            }
            return param;
        }
        #endregion
    }
}
